package com.mxpio.erp.plan.service.impl;

import com.mxpio.erp.common.enums.CommonEnums;
import com.mxpio.erp.common.enums.OrderStatusEnums.BpmnStatus;
import com.mxpio.erp.common.plan.entity.*;
import com.mxpio.erp.common.plan.service.MpsLineService;
import com.mxpio.erp.common.plan.service.MpsVersionService;
import com.mxpio.erp.common.plan.service.MpsVersionTempService;
import com.mxpio.erp.common.technology.vo.AuditOpinionVO;
import com.mxpioframework.common.util.BeanReflectionUtils;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.policy.impl.CrudType;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.security.util.SecurityUtils;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class MpsVersionTempServiceImpl extends BaseServiceImpl<MpsVersionTemp> implements MpsVersionTempService {

	@Autowired
	private MpsVersionService mpsVersionService;
	@Autowired
	private MpsLineService mpsLineService;
	@Override
	@Transactional(readOnly = false)
	public Result<MpsVersionTemp> updateBpmnStatus(MpsVersionTemp mpsVersion, BpmnStatus bpmnStatus) {
		mpsVersion.setBpmnStatus(bpmnStatus.getStatusCode());
		mpsVersion.setReviewer(SecurityUtils.getLoginUsername());
		mpsVersion.setReviewTime(new Date());
        this.update(mpsVersion);
        return Result.OK(mpsVersion);
	}

	@Override
	@Transactional(readOnly = false)
	public Result<MpsVersionTemp> auditWithOpinion(MpsVersionTemp mpsVersionTemp, AuditOpinionVO auditOpinionVO) {
		String agree = auditOpinionVO.getAgree();
		String auditOpinion = auditOpinionVO.getAuditOpinion();
		if(CommonEnums.YesNo.YES.getCode().equals(agree)){//同意
			//更新原主计划
			MpsVersion byId = mpsVersionService.getById(MpsVersion.class, mpsVersionTemp.getCode());
			byId.setChangeReason(mpsVersionTemp.getChangeReason());
			byId.setReviewOpinion(StringUtils.isBlank(auditOpinion)?"审核通过，已生效。":"审核通过，已生效。审核意见："+auditOpinion);
			byId.setReviewer(SecurityUtils.getLoginUsername());
			byId.setReviewTime(new Date());
			mpsVersionService.update(byId);//更新原主计划
			Criteria c = Criteria.create().addCriterion("versionCode", Operator.EQ, byId.getCode());
			mpsLineService.deleteBatch(MpsLine.class,c);//清空原主计划清单
			//List<MpsLineTemp> mpsLineTempList = mpsVersionTemp.getMpsLineTempList();
			List<MpsLineTemp> mpsLineTempList =
					JpaUtil.linq(MpsLineTemp.class).
							where(Criteria.create().addCriterion("pid", Operator.EQ, mpsVersionTemp.getId())).list();
			if (mpsLineTempList.size()>0){
				for (MpsLineTemp templine:mpsLineTempList){
					MpsLine mpsLine = new MpsLine();
					BeanReflectionUtils.copyProperties(mpsLine,templine);
					mpsLine.setId(null);
					mpsLine.setVersionCode(byId.getCode());
					mpsLine.setCrudType(CrudType.SAVE);
					mpsLineService.save(mpsLine);
				}
			}
			mpsVersionTemp.setBpmnStatus(BpmnStatus.AUDITED.getStatusCode());//
			mpsVersionTemp.setReviewOpinion(StringUtils.isBlank(auditOpinion)?"审核通过，已生效。":"审核通过，已生效。审核意见："+auditOpinion);
			mpsVersionTemp.setReviewer(SecurityUtils.getLoginUsername());
			mpsVersionTemp.setReviewTime(new Date());
			this.update(mpsVersionTemp);
		}else{
			mpsVersionTemp.setBpmnStatus(BpmnStatus.CLOSED.getStatusCode());//关闭
			mpsVersionTemp.setReviewOpinion(StringUtils.isBlank(auditOpinion)?"审核不通过，作废。":"审核不通过，作废。审核意见："+auditOpinion);
			mpsVersionTemp.setReviewer(SecurityUtils.getLoginUsername());
			mpsVersionTemp.setReviewTime(new Date());
			this.update(mpsVersionTemp);
		}
		return Result.OK(mpsVersionTemp);
	}
}
